<?php

namespace App\Workerman;
use App\Models\HardWareApi\TrashMessage;

/**
 *垃圾桶消息处理
 *
 */
class TrashManagement
{

	/**
	 *发送主题消息
	 *
	 * @response json {
	 *	 "imei": "设备 imei 号"
     *   "ts": "时间戳"
     *	 "cmd": "命令码 1,2,3,4,5"
     *	 "param": "参数"
     *	 "alive": "网络常连接状态，1 长连接，0 短连接"
     *	 "report_gap": "上报时间间隔,单位秒"
     *	 "shake_level": "gsensor 震动等级，0 关闭，1 低，2 中， 3 高"
     *	 "url": "http get 文件地址---硬件升级"
     *	 "soft_ver": "最前服务器中最新升级文件版本号"
     * }
	 *
	 *
	 */
	public function postMessage($cmd, $imei)
	{
		$topic = "xiaotu/postMessage/trash";
		switch ($cmd) {
			case '1':
				$message = [
					"imei" => $imei,
					"ts" => time(),
					"cmd" => 1
				];
				break;
			case '2':
				$message = [
					"imei" => $imei,
					"ts" => time(),
					"cmd" => 1,
					"param" => [
						"alive" => 1,//1 长连接，0 短连接
						"report_gap" => 60,
						"shake_level" => 1
					]
				];
				break;
			case '3':
				$message = [
					"imei" => $imei,
					"ts" => time(),
					"cmd" => 1,
					"param" => [
						"alive" => 1,//1 长连接，0 短连接
						"report_gap" => 60
					]
				];
				break;
			case '4':
				$message = [
					"imei" => $imei,
					"ts" => time(),
					"cmd" => 4
				];
				break;
			// case '5':
			// 	"imei" => $imei,
			// 		"ts" => time(),
			// 		"cmd" => 5,
			// 		"param" => [
			// 			"url" => '',
			// 			"soft_ver" => 'V1.0.1'
			// 		]
			// 	break;
			default:
				$message = [
					"imei" => $imei,
					"ts" => time(),
					"cmd" => 1
				];
				break;
		}

		return array('topic'=>$topic, json_encode($message));
	}


	/**
	 *接收主题消息
	 *
	 * @response json {
	 *	 "imei": "设备 imei 号"
     *   "ts": "时间戳"
     *	 "cmd": "命令码 1,2,3,4,5"
     *	 "dev_sta": "设备状态"
     *	 "gps_sta": "gps 定位状态，1 已定位，0 未定位"
     *	 "lon": "经度,保留 5 位小数"
     *	 "lat": "纬度,保留 5 位小数"
     *	 "lac": "小区号，用于 lbs 基站定位"
     *	 "ci": "基站号，用于 lbs 基站定位"
     *	 "report_gap": "上报时间间隔,单位秒"
     *	 "ir_sta": "红外对射状态，1 遮挡，0 未遮挡 [0,0,1]"
     *	 "distance": "超声波测量距离，单位米,保留 2 位小数"
     *	 "power_vol ": "电池电压，单位 v,保留 3 位小数"
     *	 "err": "错误码，0 成功，1 参数错误"
     *	 "iccid": "20 位 nb 卡号(字母和数字混合)"
     *	 "imsi": "15 位 imsi"
     *	 "shake_level": "gsensor 震动等级，0 关闭，1 低，2 中， 3 高"
     *	 "url": "http get 文件地址---硬件升级"
     *	 "soft_ver": "最前服务器中最新升级文件版本号"
     * }
	 *
	 *
	 */
	public function receiveMessage($message)
	{
	    $topic = "xiaotu/receiveMessage/trash";
		$message = json_decode($message);
		switch ($message['cmd']) {
			case '1':
				if($message['dev_sta']['gps_sta'] == $_SESSION['dev_sta']['gps_sta'] || $message['dev_sta']['lon'] == $_SESSION['dev_sta']['lon'] || $message['dev_sta']['lat'] == $_SESSION['dev_sta']['lat'] || $message['dev_sta']['ci'] == $_SESSION['dev_sta']['ci'] || $message['dev_sta']['ir_sta'] == $_SESSION['dev_sta']['ir_sta'] || $message['dev_sta']['distance'] == $_SESSION['dev_sta']['distance'] || $message['dev_sta']['power_vol'] == $_SESSION['dev_sta']['power_vol']) {

				    $trashMessage = TrashMessage::where('imei',$message['imei'])->first();
                    $trashMessage->update($message['dev_sta']);
				}else {
					$_SESSION['dev_sta'] = $message['dev_sta'];
				}

				break;
			case '2':
				if($message['err']){
					$message = "设置参数错误";
				}
				break;
			case '3':

				break;
			case '4':
				if($message['dev_sta']['gps_sta'] == $_SESSION['dev_sta']['gps_sta'] || $message['dev_sta']['lon'] == $_SESSION['dev_sta']['lon'] || $message['dev_sta']['lat'] == $_SESSION['dev_sta']['lat'] || $message['dev_sta']['ci'] == $_SESSION['dev_sta']['ci'] || $message['dev_sta']['ir_sta'] == $_SESSION['dev_sta']['ir_sta'] || $message['dev_sta']['distance'] == $_SESSION['dev_sta']['distance'] || $message['dev_sta']['power_vol'] == $_SESSION['dev_sta']['power_vol']) {

				    $trashMessage = TrashMessage::where('imei',$message['imei'])->first();
                    $trashMessage->update($message['dev_sta']);
				}else {
					$_SESSION['dev_sta'] = $message['dev_sta'];
				}
				return $message;
				break;
			case '5':

				break;
			default:
				if($message['dev_sta']['gps_sta'] == $_SESSION['dev_sta']['gps_sta'] || $message['dev_sta']['lon'] == $_SESSION['dev_sta']['lon'] || $message['dev_sta']['lat'] == $_SESSION['dev_sta']['lat'] || $message['dev_sta']['ci'] == $_SESSION['dev_sta']['ci'] || $message['dev_sta']['ir_sta'] == $_SESSION['dev_sta']['ir_sta'] || $message['dev_sta']['distance'] == $_SESSION['dev_sta']['distance'] || $message['dev_sta']['power_vol'] == $_SESSION['dev_sta']['power_vol']) {

				    $trashMessage = TrashMessage::where('imei',$message['imei'])->first();
                    $trashMessage->update($message['dev_sta']);
				}else {
					$_SESSION['dev_sta'] = $message['dev_sta'];
				}
				return $message;
				break;
		}

		return array('topic'=>$topic, json_encode($message));
	}
}

